home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / MATH / MATH3871 / SQWAVE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-05-13  |  5.1 KB  |  178 lines

  1. {$N+}
  2.  
  3. program sqwave;
  4.  
  5. { >>>>>>>> If you are using Turbo Pascal 6.0, change to math6387 :- }
  6. uses
  7.   dos, crt, graph, math7387;
  8.  
  9. { Program to demonstrate the speed of the MATH387 sine function.
  10.   Calculates a series of odd harmonics to plot square waves, using
  11.   the formula:
  12.                y = sin x  +  1/3 sin 3x  +  1/5 sin 5x  +  ...
  13.      order:        1             3              5          ...
  14.   The program requests the order of the calculation: 1 produces a pure
  15.   sine wave, while values above about 101 produce a near square wave.
  16.   A value of 601 or more is suggested to provide long enough run times
  17.   for meaningful time measurements on fast machines.
  18.  
  19.   Written by Chris Williams, 12 May 1995. }
  20.  
  21. var
  22.   xp, x1, x2, miny, maxy, minx, maxx, xscale, yscale : single;
  23.   i, graph_driver, graph_mode, error_code, max_x, max_y,
  24.     yoffset, order : integer;
  25.   ch : char;
  26.   timestart, num_calc : longint;
  27.   time_borland, time_math387 : real;
  28.  
  29. function now : longint;
  30. { Returns the time of call in 100th of a second }
  31. var
  32.   hour, min, sec, sec100th : word;
  33.   lhour, lmin, lsec, lsec100th : longint;
  34. begin
  35.   gettime (hour, min, sec, sec100th);
  36.   lhour := hour;
  37.   lmin := min;
  38.   lsec := sec;
  39.   lsec100th := sec100th;
  40.   now := lsec100th + lsec*100 + lmin*6000 + lhour*360000;
  41. end;  { of now }
  42.  
  43. procedure reset_timer;
  44. { Resets start of time interval for use with "timer" }
  45. begin
  46.   timestart := now
  47. end;  { of reset_timer }
  48.  
  49. function timer : real;
  50. { Outputs seconds elapsed since "reset_timer" was called }
  51. begin
  52.   timer := (now - timestart) / 100.0
  53. end;  { of timer }
  54.  
  55. function y_borland (order : integer; x : single) : single;
  56. { Odd harmonic calculation using Borland library "sin" }
  57. var
  58.   i : integer;
  59.   t : single;
  60. begin
  61.   t := 0.0;
  62.   i := 1;
  63.   while i <= order do
  64.   begin
  65.     t := t + (1.0 / i) * sin (x * i);
  66.     inc (i, 2)
  67.   end;
  68.   y_borland := t
  69. end;
  70.  
  71. function y_math387 (order : integer; x : single) : single;
  72. { Odd harmonic calculation using MATH387 inline "s_sin" }
  73. var
  74.   i : integer;
  75.   t : single;
  76. begin
  77.   t := 0.0;
  78.   i := 1;
  79.   while i <= order do
  80.   begin
  81.     t := t + (1.0 / i) * s_sin (x * i);
  82.     inc (i, 2)
  83.   end;
  84.   y_math387 := t
  85. end;
  86.  
  87. begin  { of main program }
  88.   if not test387 then
  89.     error387;
  90.   repeat
  91.     clrscr;
  92.     writeln;
  93.     writeln ('            SQWAVE version 1.0 (c) Chris Williams, 1995');
  94.     writeln;
  95.     writeln ('This program demonstrates the speed of the MATH7387 unit by plotting');
  96.     writeln ('a square wave graph calculated from adding odd harmonics.');
  97.     writeln;
  98.     repeat
  99.       writeln ('Enter an odd value for the order of the calculations');
  100.       write ('(in the range 1 to 32765, suggest about 601) : ');
  101.       readln (order)
  102.     until order >= 1;
  103.     repeat
  104.       writeln;
  105.       write ('Enter the maximum X value (suggest about 25) : ');
  106.       readln (x2);
  107.     until (x2 > 0.0) and (x2 < 10000.0);
  108.     writeln;
  109.     writeln (' >>>> The first (top) graph is drawn using the Borland library "sin"');
  110.     writeln;
  111.     writeln (' >>>> The second (bottom) graph is drawn using MATH7387 "s_sin"');
  112.     writeln;
  113.     writeln ('Press any key to draw graphs (Q to quit) ...');
  114.     ch := readkey;
  115.     if ch = #0 then
  116.       ch := readkey;
  117.     if ch in ['q', 'Q'] then
  118.       halt;
  119.     { Set graph limits }
  120.     x1 := 0.0;
  121.     maxy := 1.0;
  122.     miny := -1.0;
  123.     graph_driver := detect;
  124.     initgraph (graph_driver, graph_mode, '');
  125.     error_code := graphresult;
  126.     if error_code <> grok then
  127.     begin
  128.       writeln ('**** Error selecting graphics : "',
  129.                grapherrormsg (error_code), '"');
  130.       writeln ('     Program aborted ...');
  131.       ch := readkey;
  132.       halt (1)
  133.     end;
  134.     max_x := getmaxx;
  135.     max_y := getmaxy div 2;
  136.     yscale := max_y / (maxy - miny);
  137.     xscale := (x2 - x1) / max_x;
  138.     rectangle (0, 0, max_x, max_y);
  139.     moveto (0, max_y - round ((y_borland (order, x1) - miny) * yscale));
  140.     reset_timer;
  141.     for i := 0 to max_x do
  142.     begin
  143.       xp := i * xscale + x1;
  144.       lineto (i, max_y - round ((y_borland (order, xp) - miny) * yscale));
  145.     end;
  146.     time_borland := timer;
  147.     yoffset := max_y;
  148.     max_x := getmaxx;
  149.     max_y := getmaxy div 2;
  150.     yscale := max_y / (maxy - miny);
  151.     xscale := (x2 - x1) / max_x;
  152.     rectangle (0, yoffset, max_x, max_y + yoffset);
  153.     moveto (0, yoffset + max_y - round ((y_math387 (order, x1) - miny) * yscale));
  154.     reset_timer;
  155.     for i := 0 to max_x do
  156.     begin
  157.       xp := i * xscale + x1;
  158.       lineto (i, yoffset + max_y - round ((y_math387 (order, xp) - miny) * yscale));
  159.     end;
  160.     time_math387 := timer;
  161.     ch := readkey;
  162.     if ch = #0 then
  163.       ch := readkey;
  164.     closegraph;
  165.     num_calc := longint (max_x + 1) * ((order + 1) div 2);
  166.     writeln ('Timings for ', num_calc, ' sine calculations are:');
  167.     writeln ('  Borland library "sin" : ', time_borland : 8 : 2, ' seconds.');
  168.     writeln ('  MATH387 "s_sin"       : ', time_math387 : 8 : 2, ' seconds.');
  169.     writeln;
  170.     writeln ('Press any key to continue (Q to quit) ...');
  171.     ch := readkey;
  172.     if ch = #0 then
  173.       ch := readkey;
  174.     if ch in ['q', 'Q'] then
  175.       halt
  176.   until false
  177. end.
  178.